package com.jxm.spring.security.security;

import com.jxm.spring.security.security.domain.SysUserDetails;
import com.jxm.spring.security.security.service.SysUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @Author: jxm
 * @Description: 用户登录验证处理类
 * @Date: 2021/5/16 19:31
 * @Version: 1.0
 */
@Component
public class UserAuthenticationProvider implements AuthenticationProvider{

    @Autowired
    private SysUserDetailsService sysUserDetailsService;

    /**
     * 身份验证
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //获取用户名
        String username = (String) authentication.getPrincipal();
        //获取密码
        String password = (String) authentication.getCredentials();

        SysUserDetails sysUserDetails = (SysUserDetails)sysUserDetailsService.loadUserByUsername(username);
        if (sysUserDetails == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }

        if (!new BCryptPasswordEncoder().matches(password, sysUserDetails.getPassword())) {
            throw new BadCredentialsException("用户名或密码错误");
        }
        System.out.println("-----------"+sysUserDetails.getEnabled());
//        if (sysUserDetails.getEnabled() == 0L) {
//            throw new LockedException("用户已禁用");
//        }
        return new UsernamePasswordAuthenticationToken(sysUserDetails, password, sysUserDetails.getAuthorities());

    }

    /**
     * 支持指定的身份验证
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
